package beautiful.butterfly.drds.data_exchange.read_and_write_data.reader.util;

import beautiful.butterfly.drds.data_exchange.error_code.DataExchangeException;
import beautiful.butterfly.drds.data_exchange.read_and_write_data.reader.Key;
import beautiful.butterfly.drds.data_exchange.util.Configuration;
import beautiful.butterfly.drds.data_exchange.util.Jdbc;
import beautiful.butterfly.drds.data_exchange.util.RdbmsException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import java.util.concurrent.Callable;


public class PreCheckTask implements Callable<Boolean>
{

    private String userName;
    private String password;
    private String splitPkId;
    private Configuration configuration;


    public PreCheckTask(String userName,
                        String password,
                        Configuration configuration,

                        String splitPkId)
    {
        this.configuration = configuration;
        this.userName = userName;
        this.password = password;

        this.splitPkId = splitPkId;
    }


    public Boolean call() throws DataExchangeException
    {
        String jdbcUrl = this.configuration.getString(Key.jdbc_url);
        List<Object> querySqls = this.configuration.getList(Key.query_sql, Object.class);
        List<Object> splitPkSqls = this.configuration.getList(Key.split_key_sql, Object.class);
        List<Object> tables = this.configuration.getList(Key.table_name, Object.class);
        Connection connection = Jdbc.getConnection(jdbcUrl, this.userName, password);
        int fetchSize = 1;
        fetchSize = Integer.MIN_VALUE;
        try
        {
            for (int i = 0; i < querySqls.size(); i++)
            {

                String splitPkSql = null;
                String querySql = querySqls.get(i).toString();

                String table = null;
                if (tables != null && !tables.isEmpty())
                {
                    table = tables.get(i).toString();
                }

                /*verify executeQuery*/
                ResultSet resultSet = null;
                try
                {

                    if (i == 0)
                    {
                        resultSet = Jdbc.executeQuery(connection, querySql, fetchSize);
                    }
                } catch (Exception e)
                {
                    throw RdbmsException.asQueryException(e, querySql, table, userName);
                } finally
                {
                    Jdbc.close(resultSet, null, null);
                }
            /*verify splitPK*/
                try
                {
                    if (splitPkSqls != null && !splitPkSqls.isEmpty())
                    {
                        splitPkSql = splitPkSqls.get(i).toString();

                        if (i == 0)
                        {
                            SingleTableSplitUtil.precheckSplitPk(connection, splitPkSql, fetchSize, table, userName);
                        }
                    }
                } catch (Exception e)
                {
                    throw RdbmsException.asSplitPKException(e, splitPkSql, this.splitPkId.trim());
                }
            }
        } finally
        {
            Jdbc.close(null, connection);
        }
        return true;
    }
}
